home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 21
/
Cream of the Crop 21 (Terry Blount) (October 1996).iso
/
program
/
srcbkvt.zip
/
IMAGE.ASC
< prev
next >
Wrap
Text File
|
1996-07-08
|
11KB
|
317 lines
_Image Processing and Visual Basic_
by Don Parrish
Example 1:
Open Path$ For Binary As #1
Datarray$ = Input$(31680, #1)
Workarray$=Datarray$
Close
Example 2:
Open Path$ For Binary As #1
For Line = 1 To 165
For Point = 1 To 192
Position = Point + (Line - 1) * 192
Datarray$(Position) = INPUT(1,#1)
Next Point
Next Line
Close
Listing One
Private Sub Form_Load()
Form1.WindowState = 2
Open_File
End Sub
Private Sub Form_Paint()
'Call the plot routine
Plot_Image
End Sub
Private Sub Normal_Histo_Click()
'If an error occurs, jump to error handling routine
On Error GoTo Normal_Histogram_ErrorHandler
'Erase old histogram
Erase Histo_Array
Workarray$ = Datarray$
'Change mouse pointer's shape
Screen.MousePointer = 11
'Calculate a histogram
For Datum = 1 To 31680
Number = Asc(Mid$(Datarray$, Datum, 1))
Histo_Array(Number) = Histo_Array(Number) + 1
Next Datum
'Enable the Stretch Histogram menu item
Stretch_Histo.Enabled = -1
Plot_Histogram
Plot_Image
Screen.MousePointer = 1
Exit Sub
Normal_Histogram_ErrorHandler:
' Define MSGBOX variables
Msg = "Enter a larger value!"
DgDef = MB_OK + MB_ICONSTOP
Title = "Process Error"
' Put together a message box with all the proper components
MsgBox Msg, DgDef, Title
Save_BMP_File.Enabled = 0
Resume Normal_Histo_Ended
Normal_Histo_Ended:
Screen.MousePointer = 1
Plot_Image
End Sub
Private Sub OpenFile_Click()
'Call the open file routine
Open_File
End Sub
Private Sub Quit_Program_Click()
End
End Sub
Private Sub Save_File_Click()
'If an error occurs, jump to error handling routine
On Error GoTo ErrorHandler
'Get path and name of file from the user
Path$ = InputBox$("Enter path and file name." & Chr$(10) & Chr$(10) &
Chr$(10) & "If you want to exit," & Chr$(10) &
Chr$(10) & "press Escape" &
Chr$(10) & "or click on Cancel.", "SAVE FILE", "C:\VB\")
'Retrieve data from the file into an array
Open Path$ For Binary As #1
Put #1, , Workarray$
Close #1
'This task is completed
Exit Sub
'Error handling routine reports an error happened
ErrorHandler:
' Define MSGBOX variables
Msg = "Did NOT save!"
DgDef = MB_OK + MB_ICONSTOP
Title = "I/O Error"
' Put together a message box with all the proper components
MsgBox Msg, DgDef, Title
Resume Normal_Histo_Completed
Normal_Histo_Completed:
End Sub
Private Sub Stretch_Histo_Click()
' Declare variables
Dim Answer, DefVal, Msg, Title
'Prompt user to input lower boundary
Msg = "Enter a lower boundary value from 0 to 253." ' Set prompt.
Title = "Stretch Hitsogram Lower Limit" ' Set title.
DefVal = "0" ' Set default return value.
'Insure user answers within limits
Do
Answer1 = InputBox(Msg, Title, DefVal) ' Get user input.
Loop Until Answer1 >= 0 And Answer1 <= 253
'Tell user what was entered
MsgBox "You entered " & Chr$(10) & Chr$(10) & Answer1 ' Display message.
Lower_Limit = Answer1
'Prompt user to input upper boundary
Msg = "Enter an upper boundary value greater than" & Chr$(10) &
Chr$(10) & Lower_Limit ' Set prompt.
Title = "Stretch Hitsogram Upper Limit" ' Set title.
DefVal = "255" ' Set default return value.
'Insure user answers within limits
Do
Answer2 = InputBox(Msg, Title, DefVal) ' Get user input.
Loop Until Answer2 >= Val(Answer1) And Answer2 <= 256
'Tell user what was entered
MsgBox "You entered " & Chr$(10) & Chr$(10) & Answer2 ' Display message.
Upper_Limit = Answer2
'Setup for the histogram plot
Dim DisplayLevel, HistoPlotLevel
LoRange = Val(Lower_Limit)
HiRange = Val(Upper_Limit)
Screen.MousePointer = 11
'Clear the old histogram plot
Erase Histo_Array
'Calculate the histogram
For Lines = 1 To 165
For Points = 1 To 192
Level = Asc(Mid$(Datarray$, Points + (Lines - 1) * 192, 1))
Brightness = CInt((Level - LoRange) / (HiRange - LoRange) * 256)
If Brightness < 0 Then Brightness = 0
If Brightness > 255 Then Brightness = 255
Mid$(Workarray$, Points + (Lines - 1) * 192, 1) = Chr$(Brightness)
Histo_Array(Brightness) = Histo_Array(Brightness) + 1
Next Points
Next Lines
'Call plot routines for the histogram and image
Plot_Histogram
Plot_Image
'Change the mouse pointer shape
Screen.MousePointer = 0
'Inhibit selection of STRETCH from the drop-down menu
Normal_Histo.Enabled = -1
Stretch_Histo.Enabled = 0
Exit Sub
'Setup error routine
ERROR_HistoStretch_MESSAGE:
MsgBox "Values Out of Range. Enter New Values.", 0, "ERROR MESSAGE"
Resume ERROR_HistoStretch_HANDLER
ERROR_HistoStretch_HANDLER:
Screen.MousePointer = 0
End Sub
Private Sub Zoom_Image_In_Click()
'Declare variable
Dim SuccessFlag
'Use raster operations to double the size of the image
'located in the left hand corner of the window
If Zoom_Check = 0 Then
'Double image size beginning from upper left corner of the original
SuccessFlag = StretchBlt(Form1.hDC, 0, 0, 384, 330, Form1.hDC, 0, 0,
192, 165, &HCC0020)
'Set a flag if this is done first
Zoom_Check = 1
Else
'Double image size beginning from an offset equal in size to original
SuccessFlag = StretchBlt(Form1.hDC, 0, 0, 384, 330, Form1.hDC, 48,
41, 240, 206, &HCC0020)
End If
End Sub
Private Sub Zoom_Image_Out_Click()
'A safe way to downsize the image is to redraw it
Plot_Image
End Sub
Listing Two
'Declarations of variables that may be accessed by any program function
Global Datarray As String, Workarray As String
Global Histo_Array(255) As Integer
Global Zoom_Check As Integer
'Windows API function calls
Declare Function CreateBitmapByString% Lib "GDI" Alias "CreateBitmap" (ByVal
nWidth%, ByVal nHeight%, ByVal nPlanes%, ByVal nBitCount%, ByVal lpBits$)
Declare Function SelectObject% Lib "GDI" (ByVal hDC%, ByVal hObject%)
Declare Function CreateCompatibleBitmap% Lib "GDI" (ByVal hDC%,
ByVal nWidth%, ByVal nHeight%)
Declare Function CreateCompatibleDC% Lib "GDI" (ByVal hDC%)
Declare Function BitBlt% Lib "GDI" (ByVal hDestDC%,ByVal x%,ByVal y%,ByVal
nWidth%,ByVal nHeight%,ByVal hSrcDC%,ByVal XSrc%,ByVal YSrc%,ByVal dwRop&)
Declare Function StretchBlt% Lib "GDI" (ByVal hDC%,ByVal x%,ByVal y%,ByVal
nWidth%,ByVal nHeight%,ByVal hSrcDC%,ByVal XSrc%,ByVal YSrc%,
ByVal nSrcWidth%,ByVal nSrcHeight%,ByVal dwRop&)
'Constants and variables used in error handlers
Global Const MB_OK = 0 ' Define button
Global Const MB_ICONSTOP = 16 ' Define Icon
Global Msg, DgDef, Title ' Declare variables
'Routine used to open files
'Initialize error handler
On Error GoTo ErrorHandler_Input
'Get file path from the user
Path$ = InputBox$("Enter path and file name" & Chr$(10) & Chr$(10) &
Chr$(10) & "If you want to exit,"& Chr$(10) & Chr$(10) & "Press
Escape" & Chr$(10) & "or click on Cancel.", "OPEN FILE", "C:\VB\")
'Load data from a file and put into strings
Open Path$ For Binary As #1
Datarray$ = Input$(31680, #1)
Workarray$ = Datarray$
Close
'Clean up the form before plotting the image
Form1.Picture1.Cls
Form1.Cls
'Set flags
Form1.Save_BMP_File.Enabled = -1
Form1.Stretch_Histo.Enabled = 0
Form1.Normal_Histo.Enabled = -1
'Plot the image